package com.vxdata.utils.modules.utils.zstp;

import cn.hutool.core.collection.CollectionUtil;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

/**
 * 读取大型非csv文件
 */
public class LargeFileReadUtil {

    /**
     * 按行读取文件map
     *
     * @param filePath      本地文件路径
     * @param contentIdList 行号
     * @return
     */
    public static List<Map<String, Object>> readFileMapList(String filePath, List<String> contentIdList, String encoding) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), encoding))) {
            return readFileMapList(reader, contentIdList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 按行读取文件map
     *
     * @param reader
     * @param contentIdList 行号
     * @return
     */
    public static List<Map<String, Object>> readFileMapList(BufferedReader reader, List<String> contentIdList) {
        try {
            List<Map<String, Object>> mapList = new ArrayList<>();
            String line;
            AtomicLong lineNumber = new AtomicLong(0);

            if (CollectionUtil.isEmpty(contentIdList)) {
                while ((line = reader.readLine()) != null) {
                    lineNumber.incrementAndGet();
                    Map<String, Object> map = new LinkedHashMap<>();
                    map.put("key", String.valueOf(lineNumber));
                    map.put("value", line);
                    mapList.add(map);
                }
            } else {
                while ((line = reader.readLine()) != null) {
                    lineNumber.incrementAndGet();
                    // 只取用 contentId 这一行
                    if (contentIdList.contains(String.valueOf(lineNumber.get()))) {
                        Map<String, Object> map = new LinkedHashMap<>();
                        map.put("key", String.valueOf(lineNumber));
                        map.put("value", line);
                        mapList.add(map);
                        break;
                    }
                }
            }
            return mapList;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 读取文件内容
     *
     * @param filePath 本地文件路径
     * @param encoding 编码格式
     * @return
     */
    public static String readFile(String filePath, String encoding) {
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), encoding))) {
            return readFile(reader);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("读取文件失败, 失败原因: " + e.getMessage());
        }
    }


    /**
     * 读取文件内容
     *
     * @param reader
     * @return
     */
    public static String readFile(BufferedReader reader) {
        StringBuilder content = new StringBuilder();
        String line;
        Integer flag = 0;
        try {
            while ((line = reader.readLine()) != null) {
                content.append(line).append("\n");
                flag++;
                if (flag > 1000) {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            closeReader(reader);
        }
        return content.toString();
    }


    private static void closeReader(BufferedReader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
